package cn.tedu.manager.service.impl;

import cn.tedu.manager.exception.ServiceException;
import cn.tedu.manager.mapper.ManagerMapper;
import cn.tedu.manager.pojo.dto.ManagerDTO;
import cn.tedu.manager.pojo.dto.ManagerQueryDTO;
import cn.tedu.manager.pojo.dto.ManagerResetPWDDTO;
import cn.tedu.manager.pojo.dto.ManagerSaveDTO;
import cn.tedu.manager.pojo.entity.Manager;
import cn.tedu.manager.pojo.vo.ManagerPageListVO;
import cn.tedu.manager.pojo.vo.ManagerVO;
import cn.tedu.manager.service.ManagerService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    /**
     * 分页查询
     *
     * @param managerQueryDTO
     * @return
     */
    @Override
    public ManagerPageListVO selectPage(ManagerQueryDTO managerQueryDTO) {
        managerQueryDTO.setQueryPage(managerQueryDTO.getPage() - 1);
        managerQueryDTO.setQueryPageSize(managerQueryDTO.getPage() * managerQueryDTO.getPageSize());
        List<ManagerVO> list = managerMapper.selectPage(managerQueryDTO);
        ManagerPageListVO managerPageListVO = new ManagerPageListVO();
        managerPageListVO.setRecords(list);
        managerPageListVO.setTotal(managerMapper.selectCount(managerQueryDTO));
        managerPageListVO.setPageSize(managerQueryDTO.getPageSize());
        managerPageListVO.setCurrent(managerQueryDTO.getPage());

        return managerPageListVO;
    }

    /**
     * 编写关键字查询的sql
     *
     * @param keywords
     * @return
     */
    public String keywords(String keywords) {
        String sql = "";
        if (keywords != null && !keywords.isEmpty()) {
            sql = "name like '%" + keywords + "%' or username like '%" + keywords + "%' or phone like '%" + keywords + "%' or email like '%" + keywords + "%'";
        }

        return sql;
    }

    /**
     * 查询单个管理员
     *
     * @param managerDTO
     * @return
     */
    @Override
    public ManagerVO selectOne(ManagerDTO managerDTO) {
        return managerMapper.selectOne(managerDTO);
    }

    /**
     * 添加管理员
     *
     * @param managerSaveDTO
     */
    @Override
    public void addManager(ManagerSaveDTO managerSaveDTO) {
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerSaveDTO, manager);
        if (managerSaveDTO.getPassword() == null || managerSaveDTO.getPassword().trim().isEmpty()) {
            throw new ServiceException(422, "请填写登录密码");
        }
        manager.setPassword(setPwd(managerSaveDTO.getPassword()));
        manager.setStatus("1");
        manager.setCreateTime(new Date());
        managerMapper.insert(manager);
    }

    /**
     * 编辑管理员
     *
     * @param managerSaveDTO
     */
    @Override
    public void updateManager(ManagerSaveDTO managerSaveDTO) {
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerSaveDTO, manager);
        LambdaUpdateWrapper<Manager> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Manager::getId, managerSaveDTO.getId());
        managerMapper.update(manager, updateWrapper);
    }

    /**
     * 修改管理员状态
     *
     * @param managerDTO
     */
    @Override
    public void changeStatus(ManagerDTO managerDTO) {
        if (managerDTO.getId() == null || managerDTO.getId() == 0) {
            throw new ServiceException(422, "获取管理员数据失败");
        }
        Manager manager = managerMapper.selectById(managerDTO.getId());
        if (manager.getStatus().equals("1")) {
            manager.setStatus("0");
        } else {
            manager.setStatus("1");
        }
        managerMapper.updateById(manager);
    }

    /**
     * 删除管理员
     *
     * @param managerDTO
     */
    @Override
    public void deleteManager(ManagerDTO managerDTO) {
        if (managerDTO.getId() == null || managerDTO.getId() == 0) {
            throw new ServiceException(422, "获取管理员数据失败");
        }
        Manager manager = new Manager();
        manager.setDeleteTime(new Date());
        BeanUtils.copyProperties(managerDTO, manager);
        managerMapper.updateById(manager);
    }

    /**
     * 重置旧密码为一个新密码
     *
     * @param managerResetPWDDTO
     */
    @Override
    public void resetPassword(ManagerResetPWDDTO managerResetPWDDTO) {
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerResetPWDDTO, manager);
        manager.setPassword(setPwd(managerResetPWDDTO.getPassword()));
        managerMapper.updateById(manager);
    }

    /**
     * 加密管理员密码
     *
     * @param password
     * @return
     */
    public String setPwd(String password) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder.encode(password);
    }
}
